package com.fuang;

import com.fuang.extendsandsuper.Apple;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Consts;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by Fuang on 2017-9-5.
 */
public class TestMain {
    public static String LL = "abc";
    public static final Apple APPLE = new Apple();

    public static void main1(String[] args) {
        Integer i = 2;
        Integer j = 2;
        Integer k = 1 + 1;
        Integer l = 1;
        l = l + 1;
        String str1 = "abc";
        String str2 = "abc";
        String str4 = "ab";
        str4 = str4 + "c";
        String str3 = new String("abc");
        String str5 = null;
        String str6 = null;

        System.out.println(i);
        System.out.println(j);
        System.out.println(i == j);
        System.out.println(i == k);
        System.out.println(i == l);
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
        System.out.println(str1 == str4);
        System.out.println(str5 == str6);

        str1.intern();
        System.out.println(str1 == LL);

        List<String> stringList = new ArrayList<>();
        System.out.println(stringList.getClass().getTypeParameters());


        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        Integer d = 3;
        Integer e = 321;
        Integer f = 321;
        Long g = 3L;
        System.out.println(c == d);
        System.out.println(e == f);
        System.out.println(c == (a + b));
        System.out.println(c.equals(a + b));
        System.out.println(g == (a + b));
        System.out.println(g.equals(a + b));

        System.out.println(test());
    }

    public static int test() {
        int x;
        try {
            x = 1;
            return x;
        } catch (Exception e) {
            x = 2;
            return x;
        } finally {
            x = 3;
            System.out.println(x);
        }
    }

    public static void main3(String[] args) {
        Request request = Request.Post("https://edzq-test.crm.ap1.oraclecloud.com/salesApi/resources/latest/CUX_DEALERIN_c");
        String auth = "Basic " + new String(Base64.getEncoder().encode("admin_developer:Nubia1q2w3e".getBytes(Charset.forName("UTF-8"))),
                Consts.UTF_8);
        request.addHeader(HttpHeaders.AUTHORIZATION, auth);
        System.out.println(auth);
        String param = "{\"CUX_DATASOURCE_c\": \"CIM\", \"CUX_ACCOUNTNO_c\": \"20000466\",\"INDATACollection_c_c\":[{\"CUX_PRDSKU_c\": " +
                "\"1116938130810002\",\"CUX_NUMBER_c\": 2,\"CUX_SALESPRICE_c\": 11, \"CUX_AMOUNT_c\": 22,\"CUX_IMEI_c\": \"MTIzQVNENDU2Nzg5MDEy77ybCjEyM0FTRDQ1Njc4OTAxM++8mw==\"}," +
                "{\"CUX_PRDSKU_c\": \"NX901J_V1-DCC-305WG-S02\",\"CUX_NUMBER_c\": 1,\"CUX_SALESPRICE_c\": 13.1, \"CUX_AMOUNT_c\": 13," +
                "\"CUX_IMEI_c\": \"MTIzQVNENDU2Nzg5MDE077yb\"}]}";
        request.bodyString(param, ContentType.create("application/vnd.oracle.adf.resourceitem+json", Consts.UTF_8));

        try {
            HttpResponse httpResponse = request.connectTimeout(5000).socketTimeout(5000).execute().returnResponse();
            System.out.println(EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8));
            System.out.println((new Content(EntityUtils.toByteArray(httpResponse.getEntity()), ContentType.APPLICATION_JSON)).asString());
            System.out.println(httpResponse);
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main4(String[] args) {
        String str = "123ASD456789012；\n123ASD456789013；";
        System.out.println(new String(Base64.getEncoder().encode(str.getBytes(Consts.UTF_8)), Consts.UTF_8));
    }

    public static void main5(String[] args) {
        Request request = Request.Post("http://cdc.server.nubia.cn/api/rms/store_out.do");

        Map<String, Object> map = new HashMap<>();
        map.put("appid", 10003);
        map.put("time", System.currentTimeMillis() / 1000);
        map.put("imei", "863483032919254");
        map.put("sign", TestMain.sign(map, "QbcdFdgelr2HPP9oW766SJjqKLjgr6Jk"));

        List<NameValuePair> nvps = TestMain.mapToNameValuePair(map);
        request.bodyForm(nvps);
        try {
            HttpResponse httpResponse = request.connectTimeout(5000).socketTimeout(5000).execute().returnResponse();
            System.out.println(EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8));
            System.out.println((new Content(EntityUtils.toByteArray(httpResponse.getEntity()), ContentType.APPLICATION_JSON)).asString());
            System.out.println(httpResponse);
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Request request = Request.Post("https://sms.server.nubia.cn/send/send_msg.action");

        Map<String, Object> map = new HashMap<>();
        map.put("mobile", "15659170139");
        map.put("message", "【努比亚】 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试（退订回复TD）");
        map.put("app_id", 1713590);
        map.put("timestamp", System.currentTimeMillis() / 1000);
        map.put("sign", TestMain.sign(map, "a5aac8514d674d4b95acce1e873820d5"));

        List<NameValuePair> nvps = TestMain.mapToNameValuePair(map);
        request.bodyForm(nvps);
        try {
            HttpResponse httpResponse = request.connectTimeout(5000).socketTimeout(5000).execute().returnResponse();
            System.out.println(EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8));
            System.out.println((new Content(EntityUtils.toByteArray(httpResponse.getEntity()), ContentType.APPLICATION_JSON)).asString());
            System.out.println(httpResponse);
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static String sign(Map<String, Object> params, String appKey) {
        Set<String> _keySet = params.keySet();
        Set<String> keySet = new TreeSet<>(_keySet);
        StringBuffer sign = new StringBuffer();
        for (String key : keySet) {
            sign.append(key).append('=').append(params.get(key));
        }
        sign.append(appKey);

        return DigestUtils.sha1Hex(sign.toString());
    }

    public static List<NameValuePair> mapToNameValuePair(Map<String, Object> map) {
        List<NameValuePair> nvps = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            nvps.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
        }

        return nvps;
    }

}
